//
//  HMHomeRetweetView.swift
//  HMVVeibo23
//
//  Created by apple on 17/2/4.
//  Copyright © 2017年 apple. All rights reserved.
//

import UIKit
import YYText

class HMHomeRetweetView: UIView {
    
    // 记录转发微博的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    // 提供属性供外界赋值 -> viewModel
    var statusViewModel: HMStatusViewModel?{
        didSet{
            // 赋值
            contentLabel.attributedText = statusViewModel?.retweetContentAttributedString
            
            // 先卸载约束
            retweetViewBottomConstraint?.uninstall()
            // 判断pic_urls 是否为nil 而且count是否 > 0
            /*
                - 有配图
                    - 赋值** 一定要先赋值 要不约束可能有问题
                    - 设置约束(转发微博的bottom == 配图的bottom + 10)
                    - 显示配图
             */
            if let picUrls = statusViewModel?.homeModel?.retweeted_status?.pic_urls, picUrls.count > 0{
                // 赋值
                pictrueView.pic_urls = picUrls
                // 设置约束
                self.snp_makeConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictrueView).offset(10).constraint
                })
                // 显示配图
                pictrueView.isHidden = false
            }else {
                /*
                    - 没有配图
                        - 设置约束(转发微博的bottom == 微博内容的bottom + 10)
                        - 隐藏配图
                 
                 */
                // 设置约束
                self.snp_makeConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                // 隐藏配图
                pictrueView.isHidden = true
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        
        // 01 添加控件
        addSubview(contentLabel)
        addSubview(pictrueView)
        // 02 添加约束
        contentLabel.snp_makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.width.equalTo(HMSCREENW - 20)
        }
        
        pictrueView.snp_makeConstraints { (make) in
            make.left.equalTo(10)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            // 在配图内容已经计算好了
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        // 约束转发微博的底部
        snp_makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictrueView).offset(10).constraint
        }
        
        contentLabel.highlightTapAction = {(containerView, text, range, rect)->() in
            let result = text.string
            print(result)
            // 通过范围截图目标的字符串
            // 发送通知告知父类的控制器 在吧url 通过object 传到控制器
            // 如果最终的截取出来的内容包含http 在发送通知
        }
    
    }
    
    //MARK: - 懒加载控件
    // 内容
    private lazy var contentLabel: YYLabel = {
        //        let lab = UILabel(fontSize: HMNORMALFONTSIZE, textColor: UIColor.black)
        let lab = YYLabel()
        // 设置yylabel显示文字的最大宽度
        lab.preferredMaxLayoutWidth = HMSCREENW - 20
        // 换行
        lab.numberOfLines = 0
        // 文本行位置调整
        let modifier = YYTextLinePositionSimpleModifier()
        modifier.fixedLineHeight = 22
        lab.linePositionModifier = modifier
        return lab
    }()
    // 配图
    private lazy var pictrueView: HMHomePictureView = {
        let view = HMHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()

}
